package com.muyu.danmo.controller;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoPrintingLikes;
import com.muyu.danmo.service.IDanmoPrintingLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 印刷出版点赞Controller
 *
 * @author yangle
 * @date 2024-10-15
 */
@RestController
@RequestMapping("/printingLikes")
public class DanmoPrintingLikesController extends BaseController
{
    @Autowired
    private IDanmoPrintingLikesService danmoPrintingLikesService;

    /**
     * 查询印刷出版点赞列表
     */
//    @RequiresPermissions("system:printingLikes:list")
    @GetMapping("/list")
    public Result<TableDataInfo<DanmoPrintingLikes>> list(DanmoPrintingLikes danmoPrintingLikes)
    {
        startPage();
        List<DanmoPrintingLikes> list = danmoPrintingLikesService.selectDanmoPrintingLikesList(danmoPrintingLikes);
        return getDataTable(list);
    }

    /**
     * 导出印刷出版点赞列表
     */
//    @RequiresPermissions("system:printingLikes:export")
    @Log(title = "印刷出版点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoPrintingLikes danmoPrintingLikes)
    {
        List<DanmoPrintingLikes> list = danmoPrintingLikesService.selectDanmoPrintingLikesList(danmoPrintingLikes);
        ExcelUtil<DanmoPrintingLikes> util = new ExcelUtil<DanmoPrintingLikes>(DanmoPrintingLikes.class);
        util.exportExcel(response, list, "印刷出版点赞数据");
    }

    /**
     * 获取印刷出版点赞详细信息
     */
//    @RequiresPermissions("system:printingLikes:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(danmoPrintingLikesService.selectDanmoPrintingLikesById(id));
    }

    /**
     * 新增印刷出版点赞
     */
//    @RequiresPermissions("system:printingLikes:add")
    @Log(title = "印刷出版点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoPrintingLikes danmoPrintingLikes)
    {
        return toAjax(danmoPrintingLikesService.insertDanmoPrintingLikes(danmoPrintingLikes));
    }

    /**
     * 修改印刷出版点赞
     */
//    @RequiresPermissions("system:printingLikes:edit")
    @Log(title = "印刷出版点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoPrintingLikes danmoPrintingLikes)
    {
        return toAjax(danmoPrintingLikesService.updateDanmoPrintingLikes(danmoPrintingLikes));
    }

    /**
     * 删除印刷出版点赞
     */
//    @RequiresPermissions("system:printingLikes:remove")
    @Log(title = "印刷出版点赞", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(danmoPrintingLikesService.deleteDanmoPrintingLikesByIds(ids));
    }

    /**
     * 点赞
     * @param printingMessageId
     * @return
     */
    @GetMapping("/like/{printingMessageId}")
    public Result likeTrainingClass( @PathVariable Long printingMessageId) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUserid() == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userid = loginUser.getUserid();
//        if (userId.equals(trainingInformationId)) {
//            return Result.error("不能点赞自己");
//        }
        return danmoPrintingLikesService.likeTrainingClass(userid, printingMessageId);
    }
}
